package com.narbeshuber.tablet.lasalara.http;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class ProposeQuestionTask extends AsyncTask<String, Object, String> {

	Context lasalaraActivity;
	ProgressDialog dialog;
	String chapterKey;
	
	public ProposeQuestionTask(Context lasalaraActivity){
		this.lasalaraActivity = lasalaraActivity;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		dialog = ProgressDialog.show(lasalaraActivity, "", "Uploading questions. Please wait...", true);
	}
	
	@Override
	protected String doInBackground(String... params) {
		try {
		HttpClient httpclient = new DefaultHttpClient();    	 
    	HttpPost httppost = new HttpPost("http://1.narbeshuber.appspot.com/posequestion");
    	 
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
		nameValuePairs.add(new BasicNameValuePair("ck", params[0]));  
		nameValuePairs.add(new BasicNameValuePair("uk", params[1]));  
		nameValuePairs.add(new BasicNameValuePair("question", params[2]));  
		nameValuePairs.add(new BasicNameValuePair("answer", params[3]));  
		
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		httpclient.execute(httppost); 
		
		} catch (Exception e) {
			Log.e("ProposeQuestion", e.getMessage());
			new AlertDialog.Builder(lasalaraActivity).
				setTitle("There was a problem sending your question!").show();
		}  
		return "";  
		 	
	}
	
	 protected void onPostExecute(String result) {
			dialog.dismiss();
	 }

}
